package com.auth0.jwt;

import java.text.DecimalFormat;
import java.util.Random;

public class RandomP {
	
	private static RandomP randomP;
	
	private RandomP(){}
	
	public static int getRandomP(){
		if(randomP==null){
			randomP = new RandomP();
		}
		double bytP = 99;//随机数指定概率，这里为99%
		double sngBegin = 1;//随机数范围  开始数字
		double sngEnd = 9;//随机数范围  结束数字
		double sngPB = 1;//指定概率数字范围  开始数字
		double sngPE = 7;//指定概率数字范围  结束数字
		double randNumP=randomP.GetRndNumP(sngBegin, sngEnd, sngPB, sngPE, bytP);
		DecimalFormat df = new DecimalFormat("#");
		String randomIntStr=df.format(randNumP);
		if(randomIntStr!=null){
			return Integer.parseInt(randomIntStr);
		}
		return 1;
	}
	
	Random rand = new Random();
	/**
	 * 按照一定概率进行随机<br>
	 * <br>
	 * 
	 * @param pSngBegin
	 *            随机数范围的开始数字
	 * @param pSngEnd
	 *            随机数范围结束数字
	 * @param pSngPB
	 *            要随机的数字的开始数字
	 * @param pSngPE
	 *            要随机的数字的结束数字
	 * @param pBytP
	 *            要随机的数字随机概率
	 * @return 按照一定概率随机的数字
	 */
	public double GetRndNumP(double pSngBegin, double pSngEnd, double pSngPB, double pSngPE, double pBytP) {

		double sngPLen;

		double sngTLen; // total length

		double sngIncreased; // 需要缩放的长度

		double sngResult;

		sngPLen = pSngPE - pSngPB;

		sngTLen = pSngEnd - pSngBegin;

		if ((sngPLen / sngTLen) * 100 == pBytP) {

			return GetRandomNum(pSngBegin, pSngEnd);

		} else {

			// ((sngPLen + sngIncreased) / (sngTLen + sngIncreased)) * 100 =
			// bytP
			sngIncreased = ((pBytP / 100) * sngTLen - sngPLen) / (1 - (pBytP / 100));

			// 缩放回原来区间
			sngResult = GetRandomNum(pSngBegin, pSngEnd + sngIncreased);

			if (pSngBegin <= sngResult && sngResult <= pSngPB) {

				return sngResult;

			} else if (pSngPB <= sngResult && sngResult <= (pSngPE + sngIncreased)) {

				return pSngPB + (sngResult - pSngPB) * sngPLen / (sngPLen + sngIncreased);

			} else if ((pSngPE + sngIncreased) <= sngResult && sngResult <= (pSngEnd + sngIncreased)) {

				return sngResult - sngIncreased;

			}
		}

		return 0f;

	}

	public double GetRandomNum(double pSngBegin, double pSngEnd) {

		return (pSngEnd - pSngBegin) * Math.random() + pSngBegin;
	}
	
	public static void main(String args[]) {
		for (int i = 0; i < 1000; i++) {
	        System.out.println(RandomP.getRandomP());
		}
	}
	
}
